import { formatTDate } from '@/utils/common'
import { useContext, useEffect, useState } from 'react'
import { getAlarmSetting, queryAlarmSettingGroup, setAlarmSetting } from '@/api/alarm'
import { Message } from '@kedacom/react-next'
import { FilterContext } from '../context'
const useFilterHook = (ref: any, onFilterSearch: { (): void; (): void }) => {
	let { filterData, setFilterData } = useContext(FilterContext)
	// 展开项
	const [expandedKeys, setExpandedKeys] = useState<any>([])
	// 选择项
	const [selectedKeys, setSelectedKeys] = useState<any>([])
	// TODO：数据合并
	const [showModal, setShowModal] = useState(false)
	// CheckBoxGroup
	const [value, setValue] = useState([])
	// 全选
	const [allValue, setAllValue] = useState(false)
	// 是否自定义告警选项
	const [selfEnable, setSelfEnable] = useState(false)
	//
	const [checkedKeys, setCheckedKeys] = useState<any>([])

	const [oldData, setOldData] = useState<any>()
	/**告警项树数据 */
	const [alarmTreeData, setAlarmTreeData] = useState<any>()
	// options
	const selectOptions = [
		{ label: '严重', value: 1, width: 100 },
		{ label: '重要', value: 2, width: 100 },
		// { label: '一般', value: 3, width: 100 },
	]
	const INPUT_WIDTH = 160
	const SELECT_WIDTH = 140
	const [timeRangValue, setTimeRangValue] = useState<any>([filterData.startDate, filterData.endDate])

	/**
	 * 过滤条件改变时，更新数据
	 * @param v 更新后的值
	 * @param keyName 更新的字段
	 */
	const onChange = (v: string, keyName: string) => {
		setFilterData({
			...filterData,
			[keyName]: v,
		})
	}
	/**开启关闭自定义告警项 */
	const showAlarmAll = (type: boolean) => {
		//type为true 为开启自定义选项，默认全选
		if (type) {
			let list = []
			for (let treeValue of alarmTreeData) {
				list.push(treeValue.key)
				for (let secondTreeValue of treeValue.children) {
					list.push(secondTreeValue.key)
				}
			}
			setCheckedKeys(list)
			setSelfEnable(true)
		} else {
			setSelfEnable(false)
		}
	}
	const confirmEvent = () => {
		setSettingList()
		setShowModal(false)
	}
	const showModalEvent = (b: boolean) => {
		setCheckedKeys([])
		getSetting()
		getSettingList()
		setShowModal(b)
	}
	//获取自定义告警项各项配置
	const getSetting = () => {
		getAlarmSetting().then((res: any) => {
			setOldData(res.data)
		})
	}
	/**获取自定义告警项全部告警项 */
	const getSettingList = () => {
		queryAlarmSettingGroup().then((res) => {
			setAlarmTreeData(res?.list || [])
		})
	}
	useEffect(() => {
		if (oldData && alarmTreeData) {
			if (oldData.enable) {
				setSelfEnable(true)
				setCheckedKeys(oldData.setting)
				// :根据包含选项进行展开
				oldData?.setting && setExpandedKeys([...new Set(oldData?.setting.map((item: string) => item[0]))])
			} else {
				setSelfEnable(false)
			}
		}
	}, [alarmTreeData, oldData])
	const changeAlarmObject = (data: string[]) => {
		let value = ''
		for (let treeValue of alarmTreeData) {
			if (data && data.indexOf(treeValue.key) > -1) {
				value += treeValue.title + ';'
				for (let secondTreeValue of treeValue.children) {
					if (data && data.indexOf(secondTreeValue.key) > -1) {
						value += secondTreeValue.title + ';'
					}
				}
			}
		}
		return value
	}
	const setSettingList = () => {
		//设置
		let data = {
			setting: checkedKeys.toString(),
			enable: selfEnable ? 1 : 0,
		}
		let _o = null
		var e = null
		let oldValue = changeAlarmObject(oldData.setting)
		let newValue = changeAlarmObject(checkedKeys)
		if (!oldData.selfEnable && selfEnable) {
			// console.log(selfEnable)
			e = '开启自定义告警选项，选择告警项为“' + newValue + '”'
		} else if (oldData.selfEnable && !selfEnable) {
			e = '关闭自定义告警选项'
		} else {
			e = '自定义告警项由“' + (oldValue || '空') + '”  变更为“' + newValue + '”'
		}
		let _e = encodeURIComponent('告警管理，' + e)
		_o = encodeURIComponent('告警管理')
		setAlarmSetting({ ...data, _e, _o })
			.then((res: any) => {
				if (res.success) {
					Message.info('设置成功')
					// setShowModal(false)
					onFilterSearch && onFilterSearch()
				}
			})
			.catch(() => {})
	}
	const onTimeRangChange = (date: any, dateString: string | any[], value: any) => {
		if (dateString && dateString.length) {
			setTimeRangValue(dateString)
			setFilterData({
				...filterData,
				startDate: formatTDate(dateString[0] + ' 00:00:00'),
				endDate: formatTDate(dateString[1] + ' 23:59:59'),
			})
		}
	}

	return {
		/**过滤条件集合 */
		filterData,
		alarmTreeData, //告警配置项
		/**过滤条件改变时，更新数据 */
		onChange,
		confirmEvent,
		showModalEvent,
		onTimeRangChange,
		timeRangValue,
		INPUT_WIDTH,
		showModal,
		selectOptions,
		SELECT_WIDTH,
		value,
		setValue,
		allValue,
		setAllValue,
		checkedKeys,
		setCheckedKeys,
		expandedKeys,
		setExpandedKeys,
		selectedKeys,
		setSelectedKeys,
		selfEnable,
		setSelfEnable,
		showAlarmAll,
	}
}
export default useFilterHook
